﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessObject.DAO
{
    public class LopHocDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="namhoc"></param>
        /// <param name="khoilop"></param>
        /// <returns></returns>
        public List<LOPHOC> getByNamHocAndKhoiLop(NAMHOC namhoc, KHOILOP khoilop)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var lophocs = from lophoc
                                  in entities.LOPHOCs
                              where lophoc.NamHocId == namhoc.NamHocId &&
                              lophoc.KhoiLopId == khoilop.KhoiLopId
                              select lophoc;
                return lophocs.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="magiaovien"></param>
        /// <returns></returns>
        //public List<LOPHOC> getByMaGiaoVien(string magiaovien)
        //{
        //    using (QLHVEntities entities = new QLHVEntities())
        //    {
        //        var giaovien = (new GiaoVienDAO()).getByMaGiaoVien(magiaovien);
        //        if (giaovien == null) return new List<LOPHOC>();

        //        return giaovien.LOPHOCs.ToList();
        //    }
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<LOPHOC> getAllLopHoc()
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var list = entities.LOPHOCs.ToList();
                foreach (var obj in list)
                    if (obj.GIAOVIENCHUNHIEM != null)
                    {

                    }
                return list;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lophoc"></param>
        /// <returns></returns>
        public void insertLopHoc(LOPHOC lophoc, NAMHOC namhoc, KHOILOP khoilop, GIAOVIEN giaovien, BANLOPHOC banhoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                entities.LOPHOCs.AddObject(lophoc);
                lophoc.GIAOVIENCHUNHIEM = entities.USERs.OfType<GIAOVIEN>().FirstOrDefault(p => p.UserId == giaovien.UserId);
                lophoc.KHOILOP = entities.KHOILOPs.FirstOrDefault(p => p.KhoiLopId == khoilop.KhoiLopId);
                lophoc.NAMHOC = entities.NAMHOCs.FirstOrDefault(p => p.NamHocId == namhoc.NamHocId);
                lophoc.BanLopHocId = banhoc.BanLopHocId;
                //////////////////////////////////////
                CHUCVU _chucvu = new CHUCVU()
                {
                    LOAICHUCVU = entities.LOAICHUCVUS.FirstOrDefault(p => p.MaChucVu == "GVCN"),
                    NamHocId = lophoc.NamHocId,
                    UserId = giaovien.UserId,
                };
                entities.CHUCVUS.AddObject(_chucvu);
                entities.SaveChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lophoc"></param>
        /// <returns></returns>
        public GIAOVIEN getGiaoVienChuNhiem(LOPHOC lophoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var giaovien = entities.LOPHOCs.FirstOrDefault(p => p.LopHocId == lophoc.LopHocId).GIAOVIENCHUNHIEM;
                return giaovien;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lophoc"></param>
        /// <returns></returns>
        public LOPHOC getFullLopHoc(LOPHOC lophoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var lop = entities.LOPHOCs.FirstOrDefault(p => p.LopHocId == lophoc.LopHocId);
                if (lop.GIAOVIENCHUNHIEM != null)
                    if (lop.NAMHOC != null)
                        if (lop.KHOILOP != null)
                            return lop;
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lophoc"></param>
        /// <param name="namhoc"></param>
        /// <param name="khoilop"></param>
        /// <param name="giaovien"></param>
        public LOPHOC updateLopHoc(LOPHOC lophoc, NAMHOC namhoc, KHOILOP khoilop, GIAOVIEN giaovien)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var _lophoc = entities.LOPHOCs.FirstOrDefault(p => p.LopHocId == lophoc.LopHocId);
                _lophoc.TenLopHoc = lophoc.TenLopHoc;
                _lophoc.GIAOVIENCHUNHIEM = entities.USERs.OfType<GIAOVIEN>().FirstOrDefault(p => p.UserId == giaovien.UserId);
                _lophoc.NAMHOC = entities.NAMHOCs.FirstOrDefault(p => p.NamHocId == namhoc.NamHocId);
                _lophoc.KHOILOP = entities.KHOILOPs.FirstOrDefault(p => p.KhoiLopId == khoilop.KhoiLopId);
                entities.SaveChanges();
                return _lophoc;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="banhoc"></param>
        /// <returns></returns>
        public List<LOPHOC> getLopHocByBanHoc(BANLOPHOC banhoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var _banhoc = entities.BANLOPHOCs.FirstOrDefault(p => p.BanLopHocId == banhoc.BanLopHocId);
                if (_banhoc == null) return new List<LOPHOC>();
                var list = _banhoc.LOPHOCs.ToList();
                foreach (var obj in list)
                {
                    if (obj.GIAOVIENCHUNHIEM == null)
                    {

                    }
                }
                return list;
           }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="khoilop"></param>
        /// <returns></returns>
        public List<LOPHOC> getLopHocByKhoiLop(KHOILOP khoilop)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var list = entities.LOPHOCs.Where(p => p.KhoiLopId == khoilop.KhoiLopId).ToList();
                foreach (var obj in list)
                {
                    if (obj.GIAOVIENCHUNHIEM == null) { }
                }
                return list;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lophoc"></param>
        /// <param name="hocsinh"></param>
        /// <returns></returns>
        public string insertHocSinh(LOPHOC lophoc, HOCSINH hocsinh)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                string result = string.Empty;
                var _hocsinh = entities.USERs.OfType<HOCSINH>().FirstOrDefault(p => p.UserId == hocsinh.UserId);
                if (_hocsinh == null) result += "*Xảy ra lỗi tìm kiếm thông tin học sinh trong dữ liệu!\n";

                int index = entities.USERs.OfType<HOCSINH>().Where(p => p.MaHocSinh == hocsinh.MaHocSinh).Count<HOCSINH>();
                if (index > 1) result += "*Mã học sinh đã tồn tại\n";

                if (result != string.Empty) return result;

                _hocsinh.MaHocSinh = hocsinh.MaHocSinh;
                _hocsinh.KhoiLopId = hocsinh.KhoiLopId;

                entities.CHITIETLOPHOCs.AddObject(new CHITIETLOPHOC() { LOPHOCs_LopHocId = lophoc.LopHocId, HOCSINHs_UserId = hocsinh.UserId });
                entities.SaveChanges();
                return string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="namhoc"></param>
        /// <param name="giaovien"></param>
        /// <returns></returns>
        public LOPHOC getLopHocByNamHocAndGiaoVien(NAMHOC namhoc, GIAOVIEN giaovien)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                return entities.LOPHOCs.FirstOrDefault(p => p.NamHocId == namhoc.NamHocId && p.GiaoVienId == giaovien.UserId);
            }
        }
    }
}
